/*
 * Copyright (C) 2011 Mette Bank, Rikke Jensen, Kenneth Brodersen, Thomas Pedersen
 * 
 * This file is part of digiPECS.
 * 
 * digiPECS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * digiPECS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with digiPECS.  If not, see <http://www.gnu.org/licenses/>.
 */

package sw6.digipecs.admin;

import java.util.ArrayList;

import sw6.digipecs.R;
import sw6.digipecs.exceptions.ImageIsEmptyException;
import sw6.digipecs.model.ExtImageInSentenceBoard;
import sw6.digipecs.model.SentenceBoardList;
import sw6.digipecs.widget.admin.HelpView;
import sw6.digipecs.widget.admin.SelectLockedAdapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class LockSentenceBoardPicturesActivity extends Activity implements
		OnItemClickListener, OnClickListener {
	private Context mContext;
	private ListView mListView;
	private Button mClearButton;
	private SentenceBoardList sbList;
	private ArrayList<ExtImageInSentenceBoard> mImgsInBoard;
	private HelpView mHelpView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selectlockedpicturesmainlayout);
		initialise();
	}

	public void initialise() {
		mHelpView = (HelpView)findViewById(R.id.helpSelectLocked); 
		mHelpView.setHeadText("Lås et eller flere billeder"); 
		mHelpView.setBodyText("Her kan billede vælges som skal låses i sætnings-strippen. Låste billeder vil altid blive vist i bogen og kan ikke fjernes/overskrives af brugeren. " +
				"Vælg en position (der angiver pladsen i boardet) og vælg et billede fra den valgte situation. Alle låste billeder kan ryddes med knappen nederst"); 
		
		mClearButton = (Button) findViewById(R.id.clearLockedButton);
		mClearButton.setOnClickListener(this); 
		mListView = (ListView) findViewById(R.id.selectLockedPicturesListView);
		mListView.setOnItemClickListener(this);
		mContext = getApplicationContext();
		updateList();
	}

	public void updateList() {
		sbList = new SentenceBoardList(mContext, 4);
		sbList.restoreState();
		mImgsInBoard = sbList
				.getmImagesInBoard();
		mListView.setAdapter(new SelectLockedAdapter(mContext, mImgsInBoard));
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Intent intent = new Intent(this, SelectPictureActivity.class);
		intent.putExtra(SelectPictureActivity.POSITION, arg2);
		startActivityForResult(intent, 0); 
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		updateList(); 
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onClick(View arg0) {
		int i = 0; 
		for(ExtImageInSentenceBoard image:mImgsInBoard)
		{
			if(image.isLocked()) {
				try {
					image.unlock(i);
				} catch (ImageIsEmptyException e) {
					new RuntimeException(e);
				}
			}
			i++; 
		}
		updateList(); 
	}
}
